package com.gpframework.module.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.config.GpframeworkConfig;
import com.gpframework.common.core.domain.entity.SysUser;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.order.domain.vo.GpProductShelvesExcel;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.order.domain.GpProductShelves;
import com.gpframework.module.order.domain.vo.GpProductShelvesVo;
import com.gpframework.module.order.domain.req.GpProductShelvesReq;
import com.gpframework.module.order.domain.req.GpProductShelvesAdd;
import com.gpframework.module.order.domain.req.GpProductShelvesUpdate;
import com.gpframework.module.order.service.IGpProductShelvesService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName GpProductShelvesController
 * @Author gpframework
 * @Date 2023-06-16
 * @Version 1.0.0
 * @Description 货架信息Controller
 */
@Validated
@Api(tags = "货架信息")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/order/productShelves" )
public class GpProductShelvesController extends BaseController {

    private final IGpProductShelvesService iGpProductShelvesService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-06-16
  * @Param gpProductShelves
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('order:productShelves:list')")
    @GetMapping("/list")
    public TableDataInfo<GpProductShelvesVo> list(GpProductShelvesReq gpProductShelvesReq)
    {
        startPage();
        QueryWrapper<GpProductShelves> lqw = new QueryWrapper<GpProductShelves>();
        gpProductShelvesReq.generatorQuery(lqw,true);
        if (StringUtils.isNotBlank(gpProductShelvesReq.getProductName())) {
            lqw.eq("gp.product_name", gpProductShelvesReq.getProductName());
        }
        lqw.orderByAsc("gp.colour");
        lqw.orderByAsc("LENGTH(gp.colour)");
        List<GpProductShelvesVo> list = iGpProductShelvesService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 货架一级列表
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("货架一级列表")
    @GetMapping("/productNameList")
    public TableDataInfo<GpProductShelvesVo> productNameList(GpProductShelvesReq gpProductShelvesReq)
    {
        startPage();
        QueryWrapper<GpProductShelves> lqw = new QueryWrapper<GpProductShelves>();
        gpProductShelvesReq.generatorQuery(lqw,true);
        if (StringUtils.isNotBlank(gpProductShelvesReq.getProductName())){
            lqw.like("gp.product_name", gpProductShelvesReq.getProductName());
        }
        lqw.groupBy("gp.product_name");
        lqw.orderByAsc("LENGTH(gp.product_name)");
        lqw.orderByAsc("gp.product_name");
        List<GpProductShelvesVo> list = iGpProductShelvesService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('order:productShelves:add')" )
    @Log(title = "添加货架信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProductShelvesAdd gpProductShelvesAdd) {
        GpProductShelves gpProductShelves = Convert.convert(new TypeReference<GpProductShelves>() {}, gpProductShelvesAdd);
        return toAjax(iGpProductShelvesService.save(gpProductShelves) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('order:productShelves:edit')" )
    @Log(title = "修改货架信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpProductShelvesUpdate gpProductShelvesUpdate) {
        GpProductShelves gpProductShelves = Convert.convert(new TypeReference<GpProductShelves>() {}, gpProductShelvesUpdate);
        return toAjax(iGpProductShelvesService.updateById(gpProductShelves) ? 1 : 0);
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('order:productShelves:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpProductShelvesVo> util = new ExcelUtil<GpProductShelvesVo>(GpProductShelvesVo.class);
        return util.importTemplateExcel("货架信息");
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('order:productShelves:add')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/importShelves")
    public AjaxResult importShelves(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 导入新数据
        ExcelUtil<GpProductShelvesExcel> util = new ExcelUtil<GpProductShelvesExcel>(GpProductShelvesExcel.class);
        List<GpProductShelvesExcel> excelList = util.importCacheExcel(file.getInputStream());
        List<GpProductShelves> list = Convert.convert(new TypeReference<List<GpProductShelves>>() {}, excelList);
        // 删除旧数据
        iGpProductShelvesService.remove(new QueryWrapper<GpProductShelves>().eq("del_status", YesNoEnum.NO.getKey()));
        return iGpProductShelvesService.inserBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('order:productShelves:export')" )
    @Log(title = "导出货架信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpProductShelvesReq gpProductShelvesReq) {
        QueryWrapper<GpProductShelves> lqw = new QueryWrapper<GpProductShelves>();
        gpProductShelvesReq.generatorQuery(lqw,true);
        List<GpProductShelvesVo> list = iGpProductShelvesService.listVo(lqw);
        ExcelUtil<GpProductShelvesVo> util = new ExcelUtil<GpProductShelvesVo>(GpProductShelvesVo. class);
        return util.exportExcel(list, "productShelves" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('order:productShelves:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProductShelvesVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProductShelvesService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('order:productShelves:remove')" )
    @Log(title = "删除货架信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProductShelvesService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
